#!/bin/sh /etc/rc.common

START=88
STOP=15
USE_PROCD=1

NAME=speederv2

_log() {
	logger -p "daemon.$1" -t "$NAME" "$2"
}

has_valid_server() {
	local server
	for server in $@; do
		[ "$(uci_get $NAME $server)" = "servers" ] && return 0
	done
	return 1
}

validate_config_section() {
	uci_validate_section "$NAME" general "$1" \
		'server:uciname' \
		'daemon_user:string:root'
}

validate_server_section() {
	uci_validate_section "$NAME" servers "$1" \
        'server_ip:ipaddr:127.0.0.1' \
        'server_port:port' \
        'listen_ip:ipaddr:0.0.0.0' \
        'listen_port:port' \
        'key:string' \
        'fec_str:string' \
        'fec_timeout:uinteger' \
        'report:uinteger' \
        'fec_mode:bool:0' \
        'fec_mtu:range(100,1400)' \
        'fec_quene_len:uinteger' \
        'jitter:uinteger' \
        'interval:uinteger' \
        'random_drop:uinteger' \
        'disable_obscure:uinteger' \
        'fifo:string' \
        'decode_buf:uinteger' \
        'fix_latency:uinteger' \
        'delay_capacity:uinteger' \
        'disable_fec:uinteger' \
        'sock_buf:uinteger' \
        'log_level:range(0,6)' \
        'log_position:string' \
        'disable_color:bool:0'
}

start_instance() {
	local server="$1"

	if [ -z "$server" -o "$server" == "nil" ]; then
		return 0
	elif ! validate_server_section "$server"; then
		_log "err" "Server config validation failed."
		return 1
	fi

	/sbin/validate_data "ipaddr" "$server_ip" >/dev/null 2>&1
	[ $? -ne 0 ] && server_ip=$(nslookup "$server_ip" | \
		sed -n 's/^Address[[:space:]]*[0-9]*:[[:space:]]*\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)$/\1/p')
	if [ -z "$server_ip" ]; then
		_log "err" "Server address validation failed."
		return 1
	fi

	/sbin/validate_data "ip6addr" "$server_ip" >/dev/null 2>&1
	[ $? -eq 0 ] && server_ip="[$server_ip]"

	procd_open_instance
	procd_set_param command /usr/bin/udpspeeder
	procd_append_param command -c
	procd_append_param command -r ${server_ip}:${server_port}
	procd_append_param command -l ${listen_ip}:${listen_port} && _log "info" "listening on: ${listen_ip}:${listen_port}"
	[ -n "$key" ] && procd_append_param command --key ${key}
	[ -n "$fec_str" ] && procd_append_param command --fec ${fec_str} && _log "info" "FEC: ${fec_str}"
	[ -n "$fec_timeout" ] && procd_append_param command --timeout ${fec_timeout} && _log "info" "timeout: ${fec_timeout}"
	[ -n "$report" ] && procd_append_param command --report ${report}
	[ -n "$fec_mode" ] && procd_append_param command --mode ${fec_mode} && _log "info" "mode: ${fec_mode}"
	[ -n "$fec_mtu" ] && procd_append_param command --mtu ${fec_mtu} && _log "info" "MTU: ${fec_mtu}"
	[ -n "$fec_quene_len" ] && procd_append_param command --quene-len ${fec_quene_len}
	[ -n "$jitter" ] && procd_append_param command --jitter ${jitter}
	[ -n "$interval" ] && procd_append_param command --interval ${interval}
	[ -n "$random_drop" ] && procd_append_param command --random-drop ${random_drop}
	[ -n "$disable_obscure" ] && procd_append_param command --disable-obscure ${disable_obscure}
	[ -n "$fifo" ] && procd_append_param command --fifo ${fifo}
	[ -n "$decode_buf" ] && procd_append_param command --decode-buf${decode_buf}
	[ -n "$fix_latency" ] && procd_append_param command --fix-latency ${fix_latency}
	[ -n "$delay_capacity" ] && procd_append_param command --delay-capacity ${delay_capacity}
	[ -n "$disable_fec" ] && procd_append_param command --disable-fec ${disable_fec}
	[ -n "$sock_buf" ] && procd_append_param command --sock-buf ${sock_buf}
	[ -n "$log_level" ] && procd_append_param command --log-level ${log_level}
	[ -n "$log_position" ] && procd_append_param command --log-position ${log_position}
	[ "$disable_color"  == "1" ] && procd_append_param command --disable-color
	procd_set_param respawn
	[ -n "$daemon_user" ] && procd_set_param user "$daemon_user" && _log "info" "running from ${daemon_user} user"
	procd_set_param pidfile "/var/run/${NAME}.${server}.pid"
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "$NAME"
}

start_service() {
	if ! validate_config_section "general" ; then
		_log "err" "Config validate failed."
		return 1
	fi
	has_valid_server $server || return 1
	for srv in $server; do
		start_instance $srv
	done
}
